﻿Imports System.Web.Mail
Imports System.Web
Imports System.Data.SqlClient
Imports System.Data

Partial Class _111_UC02_010
    Inherits System.Web.UI.Page


    Function Active() As String
        Dim i, iMin, iMax, sCode
        Dim sURL, sFullURL, sBody
        sBody = ""
        iMin = Asc("a")
        iMax = Asc("z")
        Randomize()
        sCode = ""
        For i = 1 To 3
            sCode = sCode & Chr(CInt(Rnd() * (iMax - iMin)) + iMin)
            sCode &= Rnd(10)
        Next
        sURL = "http://localhost/VN/activate.aspx"

        sFullURL = sURL & "?Code=" & sCode

        sBody = sBody & "Mã kích hoạt của bạn là: " & sCode & vbCrLf
        sBody = sBody & "Để kích hoạt tài khoản "
        sBody = sBody & "Bạn vui lòng click vào đường link sau: " & vbCrLf & vbCrLf
        sBody = sBody & sFullURL & vbCrLf & vbCrLf
        sBody = sBody & "Hoặc nhấn vào đây để nhập mã kích hoạt:" & vbCrLf & vbCrLf
        sBody = sBody & sURL & vbCrLf & vbCrLf
        Return sBody
    End Function

    'Gui mail
    Function SendMail() As Boolean
        Try
            Dim objSend As New MailMessage()
            objSend.From = "ngo6ha3idang8@yahoo.com"
            objSend.To = "sadpiglet20@yahoo.com"
            objSend.BodyFormat = MailFormat.Html
            objSend.Priority = MailPriority.Normal
            objSend.Subject = "Hello there!"
            objSend.Body = Active()
            SmtpMail.SmtpServer = ""
            SmtpMail.Send(objSend)
            MsgBox("Account was sent to your mailbox, please check mail and activate your account", MsgBoxStyle.Information, "Information")
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    'Ham check account
    Function CheckAccount(ByVal string1 As String) As Boolean
        Dim intCheck As Integer
        Dim i, temp As Integer
        Dim tong As Integer = 0
        Dim count As Integer = 0
        For i = string1.Length - 1 To 0 Step -1
            intCheck = Convert.ToInt32(string1(i)) - 48
            If (count Mod 2) = 1 Then
                intCheck *= 2
                temp = intCheck
                intCheck = 0
                intCheck = Int(temp / 10)
                intCheck += temp Mod 10
            End If
            tong += intCheck
            count += 1
        Next
        If (tong Mod 10) = 0 Then
            Return 1
        End If
        Return 0
    End Function


    Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
        Dim strCheck As String = args.Value
        args.IsValid = (strCheck.Length >= 5)
    End Sub

    Protected Sub btnSubmmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmmit.Click
        Dim strErrors As String = ""
        Dim flag As Boolean = 0
        'Dim filename As String = "D:/Program/EC/eCSS - Version 01-080420/eCSS - Version 01/App_Data/practise01.mdb"
        'Using Sql server 2000 or 2005
        Dim strConnect As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Program\EC\HAIDANG_DATABASE.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
        'Using access
        'Dim strCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        Dim cn As New SqlConnection(strConnect)
        cn.Open()
        Dim cmd As New SqlCommand("select TenDangNhap from M_TAIKHOAN where TenDangNhap= '" + txtName.Text.ToString + "'", cn)
        Dim adapter As New SqlDataAdapter(cmd)
        Dim table As DataTable = New DataTable("TenDangNhap")
        adapter.Fill(table)
        cn.Close()
        If table.Rows.Count > 0 Then
            strErrors += "Name has been existed" + vbNewLine
        End If
        If CheckAccount(txtCardNumber.Text) = False Then
            strErrors += "Account number is not correct" + vbNewLine
        End If
        If txtDay.Text <> "" Then
            If Integer.Parse(txtDay.Text) > Date.DaysInMonth(ddlYear1.SelectedItem.Value, ddlMonth1.SelectedItem.Value) Then
                strErrors += "Date is not valid" + vbNewLine
            End If
        End If
        If (strErrors <> "") Then
            MsgBox(strErrors, MsgBoxStyle.Information, "Information")
            Response.Redirect("UC02-010.aspx")
            flag = False
        End If
        flag = True
        If flag = True Then
            SendMail()
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
End Class
